﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


namespace EHTWebsite.Code
{
    /// <summary>
    /// Summary description for HoTroProvider
    /// CuongBH
    /// 07/12/2011
    /// </summary>
    public class HoTroProvider
    {
        public static Int32 IUHoTro(HoTro theHoTro, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUHoTros";
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter theHoTroIDParameter = new SqlParameter();

            theHoTroIDParameter.ParameterName = "@HoTroIDOut";
            theHoTroIDParameter.Direction = ParameterDirection.Output;
            theHoTroIDParameter.DbType = DbType.Int32;

            command.Parameters.Add(theHoTroIDParameter);
            command.Parameters.Add(new SqlParameter("@HoTroID", theHoTro.HoTroID));
            command.Parameters.Add(new SqlParameter("@GhiChuVN", theHoTro.GhiChuVN));
            command.Parameters.Add(new SqlParameter("@GhiChuOther", theHoTro.GhiChuOther));
            command.Parameters.Add(new SqlParameter("@Nick", theHoTro.Nick));
            command.Parameters.Add(new SqlParameter("@Style", theHoTro.Style));
            command.Parameters.Add(new SqlParameter("@Publish", theHoTro.Publish));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theHoTro.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@HoTroIDOut"].Value.ToString());
        }

        public static void DeleteHoTroByHoTroID(Int32 HoTroID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteHoTroByHoTroID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@HoTroID", HoTroID));

            DataProvider.ExecuteNoneQuery(command);
        }

        public static DataSet GetHoTrosForBackEnd(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetHoTrosForBackEnd";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        public static DataSet GetHoTrosForFrontEnd(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetHoTrosForFontEnd";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        public static HoTro GetHoTroByHoTroID(Int32 HoTroID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetHoTroByHoTroID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@HoTroID", HoTroID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            HoTro theHoTro = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theHoTro = new HoTro();
                theHoTro.GhiChuVN = ds.Tables[0].Rows[0]["GhiChuVN"].ToString();
                theHoTro.GhiChuOther = ds.Tables[0].Rows[0]["GhiChuOther"].ToString();
                theHoTro.Nick = ds.Tables[0].Rows[0]["Nick"].ToString();
                theHoTro.Style = ds.Tables[0].Rows[0]["Style"].ToString();
                theHoTro.HoTroID = Int32.Parse(ds.Tables[0].Rows[0]["HoTroID"].ToString());
                theHoTro.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());
                theHoTro.NumberOfOrder = Int32.Parse(ds.Tables[0].Rows[0]["NumberOfOrder"].ToString());
            }

            return theHoTro;
        }

        public static void ChangePublishByHoTroID(Int32 hoTroID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByHoTroID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@HoTroID", hoTroID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void UpdateNumberOfOrderByHoTroID(Int32 hoTroID, Int32 numberOfOrder)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateNumberOfOrderByHoTroID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@HoTroID", hoTroID));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", numberOfOrder));
            DataProvider.ExecuteNoneQuery(command);
        }
    }
}